#include "StdAfx.h"
#include "PictureLoader.h"
#include "graphlibHTW.h"
#include "Picture.h"
#include "Constants.h"
#include <iostream>
using namespace std;
	
/// default ctor
PictureLoader::PictureLoader(void)
{	
}

/// opens an returns a picture file by using graphlibHTW.h, path to filename required
Picture *PictureLoader::loadPicture(string s)
{
	int w, h, bpp; 
	Picture * picture;
	pic = (unsigned char *)htwOpenImage(s.c_str(), &w, &h, &bpp);
	if (bpp!=3)
	{
		if (bpp==1)
		{
			unsigned char *newPic = (unsigned char *)htwCreateImage(FILENAME_PLACEHOLDER, w, h, 3);
			for (int i=0; i<h*w; i++)	
				newPic[i*3] = newPic[i*3+1] = newPic[i*3+2] = pic[i];						
			picture = new Picture(newPic,w,h,3);
		}
		else
			return nullptr;
	}
	else
		picture = new Picture(pic,w,h,bpp);

	return picture;
}

/// saves a picture by using graphlibHTW.h. requires picture and path where to save
bool PictureLoader::savePicture(Picture _picture, string s)
{
	return htwSaveImage(s.c_str(), _picture.pic, _picture.pWidth, _picture.pHeight, _picture.pBytesPerPixel );
}